Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  What does the following function do?int fun(i... Start Learning for Free
What does the following function do?
int fun(int x, int y)
{
if (y == 0)   return 0;
return (x + fun(x, y-1));
  • a)
    x + y
  • b)
    x + x*y
  • c)
    x*y
  • d)
    xy
Correct answer is option 'C'. Can you explain this answer?
Most Upvoted Answer
What does the following function do?int fun(int x, int y){if (y == 0) ...
Explanation:

The given function takes two integer parameters x and y and returns the product of x and y using recursion.

Algorithm:

1. Check if y is equal to 0.
2. If yes, return 0.
3. If no, call the function recursively with x and y-1 as parameters.
4. Multiply the result obtained from the recursive call with x and return the value.

Example:

Let's take an example to understand how the function works.

If x = 5 and y = 3, then the function call will be:

fun(5, 3)

Since y is not equal to 0, the function will call itself recursively with fun(5, 2).

fun(5, 2) will call fun(5, 1), which will call fun(5, 0).

Since y is now equal to 0, the function will return 0.

fun(5, 1) will now return 5 * 0, which is 0.

fun(5, 2) will return 5 * 0, which is 0.

Finally, fun(5, 3) will return 5 * 0, which is 0.

Therefore, the function will return 0 when y is equal to 0, and otherwise, it will recursively call itself with y-1 as the new parameter until y becomes 0.
Free Test
Community Answer
What does the following function do?int fun(int x, int y){if (y == 0) ...
The function adds x to itself y times which is x*y.
Explore Courses for Computer Science Engineering (CSE) exam
Question Description
What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer?.
Solutions for What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer?, a detailed solution for What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? has been provided alongside types of What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What does the following function do?int fun(int x, int y){if (y == 0) return 0;return (x + fun(x, y-1));a)x + yb)x + x*yc)x*yd)xyCorrect answer is option 'C'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev